/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.world_logic;

import csm.uttils.Archive;
import csm.uttils.generics.Roster;

/**
 *
 * @author Gaston M. Quiroga <gaston.mauricio.quiroga@gmail.com>
 */
public abstract class temp {

    public static World world;

    /**
     * Receives the path of a ctg file and return the Roster of continents
     * contained on it.
     * @param file path of the Ctg file
     * @return Rooster of continents
     */
    public static Roster<Continent> loadContinentsFromFile(String file) throws Exception {
        String[] fileArray = file.split("\\.");
        if (fileArray[fileArray.length - 1].compareToIgnoreCase("ctj") != 0) {
            throw new Exception("The file is not a valid country file. It must be a ctj File");
        }
        Archive archive = new Archive(file);
        Roster<Continent> continents = (Roster<Continent>) archive.readObject();
        return continents;

    }

    /**
     * Receives the path of a otg file and return the Roster of Objective Cards
     * contained on it.
     * @param file path of the Ctg file
     * @return Rooster of Objectives
     */
    public static Roster<ObjectiveCard> loadObjectiveCardsFrom(String file) throws Exception {
        String[] fileArray = file.split("\\.");
        if (fileArray[fileArray.length - 1].compareToIgnoreCase("otj") != 0) {
            throw new Exception("The file is not a valid country file. It must be a otj File");
        }
        Archive archive = new Archive(file);
        Roster<ObjectiveCard> objectives = (Roster<ObjectiveCard>) archive.readObject();
        return objectives;
    }

    /**
     * Receives the path of a stg file and return the Roster of situations
     * contained on it.
     * @param file path of the Ctg file
     * @return Rooster of countries
     */
    public static Roster<Situation> loadSituationsFromFile(String file) throws Exception {
        String[] fileArray = file.split("\\.");
        if (fileArray[fileArray.length - 1].compareToIgnoreCase("stj") != 0) {
            throw new Exception("The file is not a valid country file. It must be a stj File");
        }
        Archive archive = new Archive(file);
        return (Roster<Situation>) archive.readObject();
    }

    /**
     * Adds a player to the roster of players that will be playing the game
     * @param player
     */
    public void newWorld(Roster<Continent> contintinents, Roster<Situation> situations, Roster<ObjectiveCard> objectives) {
//        world = new World(contintinents, situations, objectives);
    }

    public static void addPlayer(Player player) throws Exception {
        if (world.getState().getValue() != WorldState.CREATED || world == null) {
            throw new Exception("This game is allready started or ended. Abort the"
                    + "game to start a new one");
        }
        if (world.getPlayers().count() >= 6) {
            throw new Exception("Maximum of players reached");
        }
        world.getPlayers().addLast(player);
    }

    /**
     * Return a Color object that has not been taken yet.
     * @return
     */
    public static Color getAvailableColor() {
        Roster<Color> colors = new Roster<Color>();
        colors.addLast(new Color(Color.BLACK));
        colors.addLast(new Color(Color.BLUE));
        colors.addLast(new Color(Color.GREEN));
        colors.addLast(new Color(Color.RED));
        colors.addLast(new Color(Color.WHITE));
        colors.addLast(new Color(Color.YELLOW));
        for (int i = 0; i < world.getPlayers().count(); i++) {
            colors.remove(world.getPlayers().getElementInPosition(i).getColor());
        }
        return colors.getElementInPosition((int) (Math.random() * colors.count()));

    }

    /**
     * Starts a new game. Asigns countries and objectives to players
     * @param players
     * @param continets
     * @param objectives
     */
    public static void NewGame() {
        world.newGame();
    }

    /**
     * adds the amount of armies to the specified country
     * @param country
     * @param amount
     */
    public static void addArmies(Country country, int amnt) {

        country.addArmie(amnt);

    }

    /**
     * Substract the amount of armies from the specifie country
     * @param country
     */
    public static void subArmies(Country country, int amnt) {
        country.subArmy(amnt);
    }

    /**
     * returns a random situation
     * @return
     */
    public static Situation getRandomSituation() {
        int random = (int) (Math.random() * world.getSituations().count());
        return world.getSituations().remove(world.getSituations().getElementInPosition(random));
    }

    /**
     * creates a new round in the world object. It reorders the turns and set
     * the pointer of the round in the firs turn ;)
     * @param situation situation of this new round
     */
    public static void nextRound(Situation situation) {
//        world.nextRound(situation);
    }

    /**
     * creates and resolve the attack. sets the new amount of armies en each
     * country and transfes a country if the attack is succesfull
     * @param from
     * @param to
     * @return a RESOLVED attack. DO NOT TRY TO RESOLVE IT AGAIN!
     */
//    public static Attack attack(Country from, Country to) throws Exception {
//        Attack attack = world.attack(from, to);
//        from.setArmies(attack.getAttackerCountryArmiesRemain());
//        to.setArmies(attack.getDefenderCountryArmiesRemain());
//        if (attack.isConquered()) {
////            from.subArmy();
////            to.addArmie();
//            world.transferCountry(from.getOwner(), to);
//        }
//        return attack;
//    }
    /**
     * Move armies from oe country to another.
     * @param from
     * @param to
     * @param amount
     * @return true if the movment is legal, false if it isn't.
     */
    /**
     * Returns the amount of armies for a player to add in relation to the
     * amount of his countris and the continents rewards.
     * @param player
     * @return
     */
    public static int getArmiesToAdd(Player player) {
//        return player.getTotalArmies()/2; //hacia abajo
        int armies = (player.getTotalArmies() / 2) + 1; //hacia arriba
        Roster<Continent> continents = world.getContinents();
        for (int i = 0; i < continents.count(); i++) {
            if (player.isOwnerOfContinent(continents.getElementInPosition(i))) {
                armies += continents.getElementInPosition(i).getArmiesReward();
            }
        }

        return armies;
    }

    /**
     * If the exchage is valid, removes the cards from the player and retunrns
     * the amount of armies for a payer to add
     * @param player
     * @param cards
     */
    public static int cardExchange(Player player, Roster<Card> cards) throws Exception {
        int armies = 0;
        if (world.isValidExchange(cards)) {
            int changes = player.getChanges() + 1;
            player.setChanges(changes);
            armies = changes * 5;
            if (changes == 1) {
                armies++;
            }
            for (int i = 0; i < cards.count(); i++) {
                player.getCards().remove(cards.getElementInPosition(i));
            }
        }
        return armies;
    }

    /**
     * Asigns a new country card to the player. If the player owns the country
     * it will recieve 3 armies.
     * @return the country asigned
     */
//    public static Country drawCountryCard(Player player) {
////        Country country=return world.drawCowntryCard();
//    }
    /**
     * Destroy all the objects of the game, and the status is set to
     * "not started"
     */
    public static void abortGame() {
    }

    public static void endGame() {
    }

    public static void removePlayer(Player player) {
        world.removePlayer(player);
    }

    public Country getCountryByColor(String codeRGB) {
        Roster<Country> countries = world.getCountriesFromContinents(world.getContinents());
        Country country = null;
        for (int i = 0; i < countries.count(); i++) {
            if (countries.getElementInPosition(i).getColor().getCodeRGB().compareToIgnoreCase(codeRGB) == 0) {
                country = countries.getElementInPosition(i);
                break;
            }
        }
        return country;
    }

    public Player getPlayerByColor(String codeRGB) {
        Player player = null;
        Roster<Player> players = world.getRounds().getLast().getTurns();
        for (int i = 0; i < players.count(); i++) {
            if (players.getElementInPosition(i).getColor().getCodeRGB().compareToIgnoreCase(codeRGB) == 0) {
                player = players.getElementInPosition(i);
                break;
            }
        }
        return player;
    }
}
